
Ext.ns("Ext.news")
Ext.news.ListPanel = Ext.extend(Ext.Panel,{
	cloasable : true,
	layout:'fit',
	initComponent : function(){
	//reader
	var _reader = new Ext.data.JsonReader(
			{
				root : 'list',
				totalProperty : 'totalCount',
				id : 'id',
				successProperty : '@success'
			}, 
			[ {
				name : 'newsid',
				mapping : 'newsid'
			}, {
				name : 'newstitle',
				mapping : 'newstitle'
			}, {
				name : 'newscontent',
				mapping : 'newscontent'
			}, {
				name : 'newimage',
				mapping : 'newimage'
			}, {
				name : 'createdtime',
				mapping : 'createdtime'
			}, {
				name : 'modifiedtime',
				mapping : 'modifiedtime'
			}, {
				name : 'author',
				mapping : 'author'
			}, {
				name : 'newscategory',
				mapping : 'newscategory'
			}, {
				name : 'hightlight',
				mapping : 'hightlight'
			}, {
				name : 'topmost',
				mapping : 'topmost'
			}, {
				name : 'sparefield1',
				mapping : 'sparefield1'
			}, {
				name : 'sparefield2',
				mapping : 'sparefield2'
			}, {
				name : 'sparefield3',
				mapping : 'sparefield3'
			}]);
	
	
	var _store =  new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
            url: 'midCheck/inputAction!getAllNews'
        }),
        pageSize : 20,
        reader : _reader
	});
	
	this.grid = new Ext.grid.GridPanel({
		id: this.id+'grid',
		store : _store,
		sm: this.sm,
		cm  :  new Ext.grid.ColumnModel([ new Ext.grid.RowNumberer(), {
			header : '新闻标题',
			dataIndex : 'newstitle',
			width : 70
		}, {
			header : '新闻内容',
			dataIndex : 'newscontent',
			width : 70
		}, {
			header : '创建时间',
			dataIndex : 'createdtime',
			width : 70,
			renderer:function(v, cellmeta, record, rowIndex, columnIndex, store){
					var JsonDateValue;
			        if (Ext.isEmpty(v))
			        	return ' ';
			        else if (Ext.isEmpty(v.time))
			        	JsonDateValue = new Date(v);
			        else
			        	JsonDateValue = new Date(v.time);
			        return JsonDateValue.format('Y-m-d');
				}
		}, {
			header : '修改时间',
			dataIndex : 'modifiedtime',
			width : 70,
			renderer:function(v, cellmeta, record, rowIndex, columnIndex, store){
					var JsonDateValue;
			        if (Ext.isEmpty(v))
			        	return ' ';
			        else if (Ext.isEmpty(v.time))
			        	JsonDateValue = new Date(v);
			        else
			        	JsonDateValue = new Date(v.time);
			        return JsonDateValue.format('Y-m-d');
				}
		} , {
			header : '作者',
			dataIndex : 'author',
			width : 70
		} , {
			header : '新闻类别',
			dataIndex : 'newscategory',
			width : 70
		}, {
			header : '是否加精',
			dataIndex :'hightlight',
			width : 70,
			renderer: function(v){
				var text = "";
				if(v == "")
					return "";
				if( v == 1)
					text = "加精";
				if( v == 0)
					text = "不加精";
				return text;
			}
		}, {
			header : '置顶',
			dataIndex : 'topmost',
			width : 70,
			renderer : function(v){
				var text = "";
				if(v == "")
					return "";
				if( v == 1)
					text = "置顶";
				if( v == 0)
					text = "不置顶";
				return text;
			}
		}, {
			header : '操作',
			dataIndex : 'fax',
			widht : 70,
			renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
			var brow="news_operation('"+record.get('newsid')
					+"','"+record.get('newstitle')
					+"','"+record.get('newscontent')
					+"','"+record.get('newimage')
					+"','"+record.get('author')
					+"','"+record.get('newscategory')
					+"','"+record.get('hightlight')
					+"','"+record.get('topmost')+"');return false;";
			var green = "news_delete("+record.get('newsid')+");return false;"
			var b = ' <a  onclick="'+brow+'">操作</a>&nbsp;';
			var c = '<a onclick="'+green+'">删除</a>';
			return b+c;
		}
		}]),
		bbar :  new Ext.PagingToolbar({
			pageSize : 20,
			store : _store,
			displayInfo : true,
			displayMsg : '显示 {0}-{1}条 / 共 {2} 条',
			emptyMsg : "无数据。",
			plugins        : [new Ext.ux.PageSizePlugin()],
			items : ['-']
		}),
		tbar : [{
      	  xtype:'combo',
      	  fieldLabel:'选择查询字段',
      	  id: this.id+'inqueryType',
      	  width : 80,
      	    store : new Ext.data.SimpleStore({
      	        fields : ['value', 'text'],
      	        data : [['title', '标题'], ['author', '作者'],['','请选择查询字段']]
      	    }),//填充数据
      	   emptyText : '选择查询字段',
      	   mode : 'local',//数据模式，local代表本地数据
      	   readOnly : true,//是否只读
      	   triggerAction : 'all',// 显示所有下列数据，一定要设置属性triggerAction为all
      	   allowBlank : true,//不允许为空
      	   valueField : 'value',//值
      	   displayField : 'text',//显示文本
      	   editable: false,//是否允许输入
      	   forceSelection: false,//必须选择一个选项
      	   blankText:'选择查询字段'//该项如果没有选择，则提示错误信息
        },{
        	xtype:'textfield',
        	id:this.id+'inqueryText'
        },{
        	xtype:'button',
        	text:'查询',
        	handler : function(b,e){
        		var a = b.findParentBy(function(ct, cmp) { 
        			return (ct instanceof Ext.news.ListPanel) ? true : false; 
        		});
        		var inqueryType = a.grid.getTopToolbar().findById(a.id+"inqueryType").getValue();
    			var inqueryText = a.grid.getTopToolbar().findById(a.id+"inqueryText").getValue();
    			if(inqueryType == "" && inqueryType == ""){
//    				Ext.Msg.alert("请选择查询字段并输入查询文本!");
//    				return;
    			}else if(inqueryType == ""){
    				Ext.Msg.alert("请选择查询字段!");
    				return;
    			}else if(inqueryText == ""){
    				Ext.Msg.alert("请输入查询文本!");
    				return;
    			}
    			a.grid.store.baseParams = {
    					start : 0,
    					limit : 20, 
						inqueryType : inqueryType,
						inqueryText : inqueryText
				}
    			a.grid.store.load();
        }
        }
		]
	});
	//window里包含的grid在此处添加
	this.items = this.grid;

Ext.news.ListPanel.superclass.initComponent.call(this);
}
});


/**
 * 根据id删除新闻
 * @param id
 * @return
 */
function news_delete(id){
	Ext.Ajax.request({
		url:'midCheck/inputAction!deleteNewsById',
		params : {
			pageMessage : id
	},
		success : function(a,b){
			Ext.Msg.alert('提示','删除新闻成功！');
			//更新成功之后刷新store
			var n = contentPanel.findById('newsgridpanel');
			if(n != null)
				n.grid.store.load({
					params:{
      	 				start:0,
      	 				limit: 30
				}
				});
	},
		failure : function(a,b){
			Ext.Msg.alert('提示','删除新闻失败！');
	}
		
	});
}

function news_operation(id,title,content,newimage,author,category,hightlight,top){
	var a  = Ext.getCmp("newupdatewindow");
	if(a == null ){
		a= new Ext.news.UpdateWindow({
			id : "newupdatewindow",
			closable : true,
			title : "修改新闻内容",
		});
	}
	
	a.panel.findById(a.id+'newsid').setValue(id);
	a.panel.findById(a.id+'newstitle').setValue(title);
	a.panel.findById(a.id+'author').setValue(author);
	a.panel.findById(a.id+'newscategory').setValue(category);
	a.panel.findById(a.id+'hightlight').setValue(hightlight);
	a.panel.findById(a.id+'topmost').setValue(top);
	a.panel.findById(a.id+'newscontent').setValue(content);

	//打开窗口
	a.show();
}